<?php

namespace App\Http\Controllers\Admin;

// use App\Repository\Order\OrderChannelRepository as model;
use App\Repository\GetWay\WayRepository as model;
use App\Repository\GetWay\WayParamsRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use App\Repository\UploadRepository;
use App\Api\Three\ShopA\ChongQingLianTong;

// 分销商
class DeliverIntController extends BaseController
{
  public $repoist;
  public function __construct(model $model)
  {
    $this->repoist = $model;
  }
  public function store(Request $request)
  {
    return $this->repoist->add($request);
  }
  public function index(Request $request)
  {
    $where = [];
    if (empty($request->user->role_id) || $request->user->role_id != 1) {
      $where['where']['shop_id'] = $request->user->shop_id;
    }
    if (!empty($request->get('product_code'))) {
      $where['like']['product_code'] = $request->get('product_code');
    }
    if (!empty($request->get('channel_code'))) {
      $where['like']['channel_code'] = $request->get('channel_code');
    }
    if (!empty($request->get('channel_id'))) {
      $where['where']['channel_id'] = $request->get('channel_id');
    }
    if ($request->has('status') && $request->get('status') != '') {
      $where['where']['status'] = $request->get('status');
    }
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');
    $data =  $this->repoist->index($where);
    return $data;
  }
  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['id' => $request->get('id')];
    return $this->repoist->edit($where, $request);
  }
  public function  show($id)
  {
    $where = ['id' => $id];
    //查询一条数据
    $return = $this->repoist->getOne($where, ['id','name','parame']);
    $returnData = [];
    $returna =[];
    if(!empty($return->parame)){
      $returna = strToKeyValArr($return->parame);
      $returna['name'] = $return->name;
      $returna['id'] = $return->id;
    }
    if (!empty($return)) {
      // $return = usfirstArr($return);
      // 将文本模式 格式化
      $model = new WayParamsRepository();
      $res = $model->getOne(['way_id'=>$return['id']]);
      if(!$res){
        return $this->returnMsg(100,'请联系开发者同学');
      }
      $returnData = strToKeyValArr($res->parame);
      $returnData['channel_code'] = $res['channel_code'];
      $returnData['channel'] = $res['channel'];
      $returnData['way'] = 'LianTongZop';
    }
    $returnData = array_merge($returna,$returnData);
    
    return $this->returnMsg(200, '查询成功', $returnData);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['name' => $request->get('name')];
    }

    $return = $this->repoist->lists($where);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    // return $this->returnMsg(201, '不能删除数据');
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->del($where);
  }
  // 导入功能
  public function import(Request $request)
  {
    $model = new UploadRepository();
    $res = $model->upload($request, 'file');
    if (empty($res['file_src'])) {
      return [
        'code' => 100,
        'msg' => '上传失败',
        'fileName' => ''
      ];
    }
    $request['file_name'] = $res['file_name'];
    $request['upload_rout'] = $res['file_src'];

    return $this->repoist->add($request);
  }
  public function getResourceid(Request $request){
    // return $this->returnMsg(200,'获取成功',['resourceId'=> '19565888888888']);
    $params = $request->all();
    // var_dump($params,111111111);
    $url = 'http://cd.10010.com/zop';;
    $paramss = 'appCode:'.$params['appCode'].';HMACMD5:'.$params['HMACMD5'].';aes:'.$params['aes'].';channel:'.$params['channel'].';product:;referrerCode:;';
    $img = public_path($params['main_pic']);
    $model = new ChongQingLianTong($url,$paramss);
    $data = [
    
      'channel_code' => $params['channel_code'],
  
      'channel' => $params['channel'],
  
      'file_name' => $img,
  
    ];
    // var_dump($data,1111111);
    $res = $model ->upload($data);
    if(!empty($res) && $res['code'] != 200){
      return $this->returnMsg(100,!empty($res['message'])?$res['message']:'获取失败,请联系技术员查看具体失败原因');
    }
    $resourceId = (!empty($res['data']) && !empty($res['data']['body']) && !empty($res['data']['body']) && !empty($res['data']['body']['resourceId']))?$res['data']['body']['resourceId']:'';
    if(empty($resourceId)){
      return $this->returnMsg(100,'获取失败,请联系技术员查看具体失败原因');
    }
    return $this->returnMsg(200,'获取成功',['resourceId'=> $resourceId]);
  }
  public function getChannelName(){
    return [
      ['id'=>'LianTongZop','name'=>'联通通用接口']
    ];
  }
}
